﻿/**
 * @file common.c
 * @brief this is a brief description
 * @details this is the detail description.
 * @author fulong 
 * @data 7/4/2014
 * @version 1.0
 * @par Copyright (c):
 * @par First build
 *     version: fulong 
 *     data: 7/4/2014
 */
#include "common.h"
#ifdef MODBUS_ASCII_MODE
#include "../ascii/ascii.h"
#endif
#ifdef MODBUS_RTU_MODE
#include "../rtu/rtu.h"
#endif
#include "../ms_protocol/slave.h"
#include "../ms_protocol/master.h"
#include "../mb_time_manage/mb_time.h"
#include "../../../dev/uart.h"

#ifdef MODBUS_ENABLE

#define modbus_send_flag macro_link(macro_link(macro_link(tx,MODBUS_UART),_buffer),.tx_done)
#define modbus_send_buffer macro_link(macro_link(tx,MODBUS_UART),_buffer)
extern uart_tx_buffer modbus_send_buffer;

modbus_operation_struct modbus_operation;
#define modbus_start_init_function(mode) mode##_mode_init
#define modbus_receive_function(mode) mode##_receive
#define modbus_send_function(mode) mode##_send
#define modbus_adjust_function(mode) mode##_adjust_verify
#define modbus_normal_rsp_function(mode) mode##_normal_rsp
#define modbus_except_rsp_function(mode) mode##_except_rsp
#define modbus_receive_frame_function(mode) mode##_receive_frame_function

#ifdef MODBUS_ASCII_MODE
#define  modbus_operation_init() modbus_start_init_function(ascii)
#define  modbus_operation_receive() modbus_receive_function(ascii)
#define  modbus_operation_send() modbus_send_function(ascii)
#define  modbus_operation_adjust() modbus_adjust_function(ascii)
#define  modbus_operation_normal_rsp() modbus_normal_rsp_function(ascii)
#define  modbus_operation_except_rsp() modbus_except_rsp_function(ascii)
#define  modbus_operation_receive_frame() modbus_receive_frame_function(ascii)
#endif
#ifdef MODBUS_RTU_MODE
#define  modbus_operation_init() modbus_start_init_function(rtu)
#define  modbus_operation_receive() modbus_receive_function(rtu)
#define  modbus_operation_send() modbus_send_function(rtu)
#define  modbus_operation_adjust() modbus_adjust_function(rtu)
#define  modbus_operation_normal_rsp() modbus_normal_rsp_function(rtu)
#define  modbus_operation_except_rsp() modbus_except_rsp_function(rtu)
#define  modbus_operation_receive_frame() modbus_receive_frame_function(rtu)
#endif

/*! \brief modbus开始时必须要做的事情,配置协议操作函数,从站初始化等等.
 * \param
 * \retrun
 */
void modbus_start_configure(void) {
    modbus_operation.modbus_init = modbus_operation_init();
    modbus_operation.modbus_receive = modbus_operation_receive();
    modbus_operation.modbus_send = modbus_operation_send();
    modbus_operation.adjust_verify = modbus_operation_adjust();
    modbus_operation.normal_rsp = modbus_operation_normal_rsp();
    modbus_operation.except_rsp = modbus_operation_except_rsp();
    modbus_operation.receive_frame_function = modbus_operation_receive_frame();
    slave_configure();
}
/*! \brief 协议的对外接收接口
 * \param
 * \retrun
 */
void modbus_receive(void) {
    slave_receive();
}
/*! \brief 函数的对外处理接口
 * \param
 * \retrun
 */
void modbus_process(void) {
    slave_process();
}
uint8 get_rsp_status(void) {
    return modbus_send_flag;
}
void reset_rsp_status(void) {
    modbus_send_flag = 0;
}
#endif
